const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
const errorHandle = require('../app/errorHandle')
const userRouter = require('../router/user.router')
const homeRouter = require('../router/home.router')
const detailRouter = require('../router/detail.router')
const categoryRouter = require('../router/category.router')
const cartRouter = require('../router/cart.router')
const addressRouter = require('../router/address.router')
const paymentRouter = require('../router/payment.router')
const OrdersRouter = require('../router/orders.router')
const fileRouter = require('../router/file.router')
const cors = require('koa2-cors')
const app = new Koa()

app.use(cors())
app.use(bodyParser())

app.use(userRouter.routes()); // 启动路由
app.use(userRouter.allowedMethods());
app.use(homeRouter.routes());
app.use(homeRouter.allowedMethods());
app.use(detailRouter.routes());
app.use(detailRouter.allowedMethods());
app.use(categoryRouter.routes());
app.use(categoryRouter.allowedMethods());
app.use(cartRouter.routes());
app.use(cartRouter.allowedMethods());
app.use(addressRouter.routes());
app.use(addressRouter.allowedMethods());
app.use(paymentRouter.routes());
app.use(paymentRouter.allowedMethods());
app.use(OrdersRouter.routes());
app.use(OrdersRouter.allowedMethods());
app.use(fileRouter.routes());
app.use(fileRouter.allowedMethods());

// 错误统一处理
app.on('error', errorHandle)
module.exports = app